package cn.stylefeng.guns.modular.robot.cmd.impl;

import cn.stylefeng.guns.modular.robot.cmd.abs.AbstractCmd;
import cn.stylefeng.guns.modular.robot.entity.Group;
import cn.stylefeng.guns.modular.robot.entity.OrderBatch;
import cn.stylefeng.guns.modular.robot.service.GroupService;
import cn.stylefeng.guns.modular.robot.service.OrderBatchService;
import cn.stylefeng.guns.modular.robot.utils.RobotUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@Transactional
public class CmdOpenUserName extends AbstractCmd {

    @Resource
    private OrderBatchService batchService;
    @Resource
    private GroupService groupService;

    @Override
    public void exec(Update update, TelegramLongPollingBot bot) {
        Long chatId = update.getMessage().getChatId();
        Group group = groupService.getGroupByTgGroupId(chatId);
        if (Objects.isNull(group) || !Boolean.TRUE.equals(group.getBatchStatus())) {
            return;
        }
        List<OrderBatch> orderBatches = batchService.lambdaQuery()
                .eq(OrderBatch::getBatchId, group.getBatchId())
                .list();
        if (CollectionUtils.isEmpty(orderBatches)) {
            return;
        }
        for (OrderBatch orderBatch : orderBatches) {
            orderBatch.setShowOderUser(true);
        }
        batchService.updateBatchById(orderBatches);
        RobotUtils.sendText(chatId, "开启名字记录,可以在账单中加入入账和下发人的名字！", bot);
    }

    @Override
    public String getCmdName() {
        return "开启名字记录";
    }

    @Override
    public String getDescribe() {
        return "开启名字记录             可以在账单中加入入账和下发人的名字\n";
    }
}
